package com.zds.hj.poseidon.biz.dispatcher.credit.demo4;

import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.common.facade.RequestBase;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.ModuleTag;
import com.zds.hj.competency.facade.management.dto.developer.PlatformBasicDto;
import com.zds.hj.competency.facade.management.dto.protocol.ProtocolInsRespDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.invoice.InvoiceAuthorizedReqDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.invoice.InvoiceAuthorizedRespDto;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.biz.common.CompetencyServiceInterface;
import com.zds.hj.poseidon.biz.common.helper.ProcessAgreementHandleService;
import com.zds.hj.poseidon.biz.dispatcher.credit.CreditCheckAbsHandler;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@DispatcherHandler(value = "CreditInvoiceAcceptHandler", desc = "税务授权模块", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CreditInvoiceAcceptHandler extends CreditCheckAbsHandler {

    @Resource
    private ProcessAgreementHandleService processAgreementHandleService;
    @Resource
    private CreditApplyService creditApplyService;
    @Resource
    private CompetencyServiceInterface competencyServiceInterface;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {
        log.info("税务授权开始===========================================================");
        //保存回调地址
        dbData.setTaxUrl(reqData.getTaxUrl());
        Date date = new Date();
        //保存税务授权日期
        dbData.setAuthorizationDate(date);
    }

    @Override
    public Object echoDetail(BaseReqModel params){

        CreditInfoEntityDto creditOnTransit = creditApplyService.findCreditOnTransit(params);
        //获取当前日期
        LocalDate currentDate = LocalDate.now();
        //进行协议内容填充
        Map<String, Object> fillData = new HashMap<>(10);
        fillData.put("legalName",creditOnTransit.getName());
        fillData.put("productAndBody",creditOnTransit.getProductName()  );
        fillData.put("legalType","中国居民身份证");
        fillData.put("legalCode", creditOnTransit.getCardId());
        fillData.put("year",currentDate.getYear());
        fillData.put("month",currentDate.getMonthValue());
        fillData.put("day",currentDate.getDayOfMonth());
        fillData.put("authorizer",creditOnTransit.getEntName());
        fillData.put("creditCode",creditOnTransit.getCompanySocialCredit());
        //获取填充后的协议信息及流水号
        ProtocolInsRespDto protocolInsRespDto = processAgreementHandleService.agreementFillHandle(params, creditOnTransit.getBatchNo(), fillData);
        creditOnTransit.setBatchNo(protocolInsRespDto.getBatchNo());

        creditApplyService.update(params,creditOnTransit);
        //获取商家信息
        RequestBase requestBase = new RequestBase();
        PlatformBasicDto platformInfo = competencyServiceInterface.findPlatformInfo(requestBase);
        //返回商家信息及协议
        Map<String,Object> map = new HashMap<>(2);
        map.put("platformInfo",platformInfo);
        map.put("protocolIns",protocolInsRespDto);
        return map;
    }

    @Override
    public Object redoPreHanding(BaseReqModel params) {
        log.info("重新预处理=======================================================");
        CreditInfoEntityDto credit = creditApplyService.findCreditOnTransit(params);

        if (EmptyChecker.isEmpty(credit)) {
            throw new BusinessException("授信信息不存在", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        //获取税务授权回调地址
        InvoiceAuthorizedReqDto reqDto = new InvoiceAuthorizedReqDto();
        reqDto.setOrderId(String.valueOf(params.getBizNo()));
        reqDto.setTaxpayerId(UUID.randomUUID().toString());
        InvoiceAuthorizedRespDto invoiceAuthorizedRespDto = competencyServiceInterface.entAuthorizedAddress(reqDto);
        //返回回调地址和批次号
        Map<String,Object> map = new HashMap<>(2);
        map.put("taxUrl",invoiceAuthorizedRespDto.getAuthUrl());
        map.put("batchNo", credit.getBatchNo());
        return map;
    }

    @Override
    public Object paramsFerry(Object o) {
        CreditInfoEntityDto dbDto = (CreditInfoEntityDto) o;
        CreditInfoEntityDto creditInfoEntityDto = new CreditInfoEntityDto();
        creditInfoEntityDto.setOrderNo(dbDto.getOrderNo());
        creditInfoEntityDto.setBatchNo(dbDto.getBatchNo());
        creditInfoEntityDto.setTaxUrl(dbDto.getTaxUrl());
        return creditInfoEntityDto;
    }

    @Override
    public ModuleTag moduleTags() {
        ModuleTag moduleTag = super.moduleTags();
        moduleTag.setContractAuth(true);
        return moduleTag;
    }
}
